//package com.lyy.demo.widget
//
//import android.content.Context
//import android.graphics.*
//import android.util.AttributeSet
//import android.view.View
//import androidx.annotation.ColorRes
//import com.blankj.utilcode.util.ConvertUtils
//import com.lyy.demo.R
//import com.lyy.demo.bean.PieChartData
//import kotlin.math.cos
//import kotlin.math.sin
//
//
///**
// * @CreateData:     2019/11/15 17:48
// * @E-Mail:         709674174@qq.com
// * @Author:         lyy
// * @Description:    圆形饼状图
// */
//class PieChartView(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {
//    private var mWidth: Int = 0
//    private var mHeight: Int = 0
//    private val mValues = ArrayList<PieChartData>()
//    @ColorRes
//    private val colors = arrayOf(R.color.colorAccent, R.color.colorAccent, R.color.colorAccent)
//    private var startAngle = 0f //开始角度
//    private val paint = Paint()
//    private var _R = 0f
//    private var r = 0f
//    private var middleR = 0f
//    private var radius = 55f
//    private var sumValue = 0f
//    private var textPaint = Paint() //画图例文字
//    private var lablePaint = Paint() //饼块上文字
//    private var rightOffset = 0f //圆从中心向右的偏移量
//    //图例第一个小矩形左上点坐标
//    private var legendX = 0f
//    private var legendY = 0f
//    //饼块中文字相关
//    private var textWidthHalf = 0f
//    private var textHeightHalf = 0f
//    private var textX = 0f
//    private var textY = 0f
//
//
//    init {
//        paint.isAntiAlias = true
//        paint.style = Paint.Style.FILL
//
//        textPaint.color = Color.BLACK
//        textPaint.textSize = 34f
//
//        lablePaint.color = Color.BLACK
//        textPaint.textSize = 28f
//
//        _R = ConvertUtils.dp2px(radius).toFloat()
//        r = ConvertUtils.dp2px(radius - 27f).toFloat()
//        middleR = ConvertUtils.dp2px(radius - 20f).toFloat()
//
//        textWidthHalf = textPaint.measureText("30") / 2 - 3
//        textHeightHalf = textPaint.measureText("30") / 2 - 3
//    }
//
//
//    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
//        mWidth = MeasureSpec.getSize(widthMeasureSpec)
//        mHeight = MeasureSpec.getSize(heightMeasureSpec)
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
//    }
//
//    override fun onDraw(canvas: Canvas) {
//        super.onDraw(canvas)
//        if (mValues.isEmpty()) {
//            return
//        }
//        val currentAngle = startAngle
//        canvas.translate((mWidth / 2).toFloat(), (mHeight / 2).toFloat())
//        val rect = RectF(rightOffset.minus(_R), -_R, rightOffset.minus(_R), _R)
//        if (sumValue == 0) {
//            paint.color = Color.parseColor("#CCCCCC")
//            canvas.drawCircle(0f, 0f, _R, paint)
//        } else {
//            var textAngle: Float
//            mValues.forEach {
//                paint.color = it.color
//                canvas.drawArc(rect, currentAngle, it.angle, true, paint)
//
//                textAngle = currentAngle.plus(it.angle / 2)
//                drawPicLabel(textAngle)
//
//                canvas.drawText(it.title, x, y, lablePaint)
//                currentAngle.plus(it.angle)
//            }
//        }
//        paint.color = Color.parseColor("#AAFFFFFF")
//        canvas.drawCircle(rightOffset, 0f, middleR, paint)
//        paint.color = Color.WHITE
//        canvas.drawCircle(rightOffset, 0f, r, paint)
//    }
//
//    /**
//     * 画每个饼块的文字
//     */
//    private fun drawPicLabel(textAngle: Float) {
//        val percent = 0.85f
//        when (textAngle) {
//            in 0f..89f -> {
//                x = (percent * _R * cos(Math.toRadians(textAngle.toDouble()))).plus(rightOffset).minus(textWidthHalf).toFloat()
//                y = (percent * _R * sin(Math.toRadians(textAngle.toDouble()))).plus(textHeightHalf).toFloat()
//            }
//            in 90f..180f -> {
//                x = (-0.75 * _R * cos(Math.toRadians(180f.minus(textAngle).toDouble()))).plus(rightOffset).minus(textWidthHalf).toFloat()
//                y = (percent * _R * sin(Math.toRadians(180f.minus(textAngle).toDouble()))).plus(textHeightHalf).toFloat()
//            }
//            in 181f..270f -> {
//                x = (-percent * _R * cos(Math.toRadians(textAngle.minus(180).toDouble()))).plus(rightOffset).minus(textWidthHalf).toFloat()
//                y = (-percent * _R * sin(Math.toRadians(textAngle.minus(180).toDouble()))).plus(textHeightHalf).toFloat()
//            }
//            else -> {
//                x = (percent * _R * cos(Math.toRadians(360f.minus(textAngle).toDouble()))).plus(rightOffset).minus(textWidthHalf).toFloat()
//                y = (-percent * _R * sin(Math.toRadians(360f.minus(textAngle).toDouble()))).plus(textHeightHalf).toFloat()
//            }
//        }
//    }
//
//    /**
//     * 画图例
//     */
//    private fun drawLegend(canvas: Canvas) {
//        val rectF1 = RectF(legendX, legendY, legendX + 30, legendY + 30)
//        paint.color = colors[0]
//        canvas.drawRect(rectF1, paint)
//
//        val rectF2 = RectF(legendX, legendY + 70, legendX + 30, legendY + 100)
//        paint.color = colors[1]
//        canvas.drawRect(rectF2, paint)
//
//        val rectF3 = RectF(legendX, legendY + 140, legendX + 30, legendY + 170)
//        paint.color = colors[2]
//        canvas.drawRect(rectF3, paint)
//
//
//        canvas.drawText("有效完成次数", legendX + 50, legendY + 27, textPaint)
//        canvas.drawText("无效完成次数", legendX + 50, legendY + 97, textPaint)
//        canvas.drawText("逾期未办次数", legendX + 50, legendY + 167, textPaint)
//    }
//
//    fun setStartAngle(angle: Float) {
//        startAngle = angle
//        invalidate()
//    }
//
//    fun setData(data: List<PieChartData>) {
//        mValues.clear()
//        mValues.addAll(data)
//        initData(mValues)
//        invalidate()
//    }
//
//    private fun initData(data: List<PieChartData>) {
//        if (data.isEmpty()) {
//            return
//        }
//        for (i in data.indices) {
//            val pieData = data[i]
//            sumValue += pieData.proportion // 求和
//            pieData.color = colors[i]   // 设置颜色
//        }
//        for (i in data.indices) {
//            val pieData = data[i]
//
//            val percentage = pieData.proportion / sumValue  // 百分比
//            val angle = percentage * 360  // 对应的角度
//
//            pieData.setPersent(percentage)
//            pieData.angle = angle
//        }
//    }
//
//    /**
//     * 获取字符串文字高度
//     */
//    private fun getTextHeight(paint: Paint, text: String): Int {
//        val rect = Rect()
//        paint.getTextBounds(text, 0, text.length, rect)
//        return rect.height()
//    }
//
//    /**
//     * 获取字符串文字长度
//     *
//     */
//    private fun getTextWidth(paint: Paint, text: String): Int {
//        return paint.measureText(text).toInt()
//    }
//}